package com.xinpu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinpu.entity.TCompanyCdrText;
import com.xinpu.entity.TCompanyCdrVideoImg;
import com.xinpu.service.ITCompanyCdrClassifyService;
import com.xinpu.service.ITCompanyCdrTextService;
import com.xinpu.service.ITCompanyCdrVideoImgService;
import com.xinpu.vo.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * CdrController
 *
 * @author ayuan
 * @date 2021/6/2
 */
@RestController
@RequestMapping("/cdr")
public class CdrController {

    @Autowired
    private ITCompanyCdrTextService itCompanyCdrTextService;

    @Autowired
    private ITCompanyCdrVideoImgService itCompanyCdrVideoImgService;

    @Autowired
    private ITCompanyCdrClassifyService itCompanyCdrClassifyService;

    /**
     * 素材库文本列表
     *
     * @param page
     * @param size
     * @param name
     * @return
     */
    @GetMapping("/list")
    public ResponseMessage list(
            @RequestParam("page") Integer page,
            @RequestParam("size") Integer size,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "cid", required = false) Integer cid
    ) {
        QueryWrapper<TCompanyCdrText> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(name)) {
            queryWrapper.like("content", name);
        }
        if (cid != null && !cid.toString().equals("")) {
            queryWrapper.like("cid", cid);
        }
        queryWrapper.orderByDesc("id");
        Page<TCompanyCdrText> textPage = itCompanyCdrTextService.page(new Page<>(page, size), queryWrapper);
        return ResponseMessage.ok(textPage);
    }

    /**
     * 创建和编辑素材库文本
     *
     * @param map
     * @return
     */
    @PostMapping("/createOrEdit")
    public ResponseMessage createOrEdit(@RequestBody Map<String, String> map) {
        TCompanyCdrText tCompanyCdrText = new TCompanyCdrText();
        String id = map.get("id");
        if (StringUtils.isNotBlank(id)) {
            tCompanyCdrText.setId(Integer.parseInt(id));
        }
        tCompanyCdrText.setContent(map.get("content"));
        tCompanyCdrText.setTitle(map.get("title"));
        if (StringUtils.isNotBlank(map.get("cid"))) {
            tCompanyCdrText.setCid(new Integer(map.get("cid")));
        }
        itCompanyCdrTextService.saveOrUpdate(tCompanyCdrText);
        return ResponseMessage.ok();
    }

    /**
     * 删除素材库文本
     *
     * @param map
     * @return
     */
    @PostMapping("/delete")
    public ResponseMessage delete(@RequestBody Map<String, String> map) {
        itCompanyCdrTextService.removeById(map.get("id"));
        return ResponseMessage.ok();
    }


    /**
     * 图片或视频库列表
     *
     * @param page
     * @param size
     * @param type
     * @return
     */
    @GetMapping("/imgOrVideoList")
    public ResponseMessage imgOrVideoList(
            @RequestParam("page") Integer page,
            @RequestParam("size") Integer size,
            @RequestParam("type") Integer type,
            @RequestParam(value = "cid", required = false) Integer cid,
            @RequestParam(value = "name", required = false) String name
    ) {
        QueryWrapper<TCompanyCdrVideoImg> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(name)) {
            queryWrapper.like("content", name);
        }
        if (cid != null && !cid.toString().equals("")) {
            queryWrapper.like("cid", cid);
        }
        queryWrapper.eq("type", type);
        queryWrapper.orderByDesc("id");
        Page<TCompanyCdrVideoImg> page1 = itCompanyCdrVideoImgService.page(
                new Page<>(page, size), queryWrapper);
        return ResponseMessage.ok(page1);
    }

    /**
     * 增加新增图片或视频库
     *
     * @param map
     * @return
     */
    @PostMapping("/addImgOrVideo")
    public ResponseMessage add(@RequestBody Map<String, String> map) {
        TCompanyCdrVideoImg tCompanyCdrVideoImg = new TCompanyCdrVideoImg();
        String id = map.get("id");
        if (StringUtils.isNotBlank(id)) {
            tCompanyCdrVideoImg.setId(new Integer(id));
        }
        tCompanyCdrVideoImg.setUrl(map.get("url"));
        tCompanyCdrVideoImg.setType(Integer.parseInt(map.get("type")));
        if (StringUtils.isNotBlank(map.get("cid"))) {
            tCompanyCdrVideoImg.setCid(new Integer(map.get("cid")));
        }
        itCompanyCdrVideoImgService.saveOrUpdate(tCompanyCdrVideoImg);
        return ResponseMessage.ok();
    }

    /**
     * 删除
     *
     * @param map
     * @return
     */
    @PostMapping("/deleteImgOrVideo")
    public ResponseMessage deleteImgOrVideo(@RequestBody Map<String, String> map) {
        itCompanyCdrVideoImgService.removeById(map.get("id"));
        return ResponseMessage.ok();
    }
}
